package cm.mefobe.android.telecomcost.entities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateUtils;

public class Day extends DayRange {
	private int _dayOfMonth;
	private int _dayOfWeek;
	private int _weekOfMonth;
	private int _monthOfYear;
	private int _year;

	public Day(int dayOfMonth,int monthOfYear,int year) {
		super(new Day(dayOfMonth),new Day(dayOfMonth));
		
		setDayOfMonth(dayOfMonth);
		setMonthOfYear(monthOfYear);		
		setYear(year);
	}
	
	public int getWeekOfYear() {
		Calendar cal  = Calendar.getInstance();
		cal.setTime(getDate());
		return cal.get(Calendar.WEEK_OF_YEAR);
	}
	
	public Day() {
		super();
	}
	
	@Override
	public Day getFirstDay() {
		return this;
	}

	@Override
	public Day getLastDay() {
		return this;
	}

	public static Day today() {
		Calendar cal = Calendar.getInstance();
		return new Day(cal.get(Calendar.DAY_OF_WEEK));
	}

	public boolean isInDays(ArrayList<Day> days) {
		int index = 0;
		while (index < days.size()) {
			if (days.get(index).getName() == getName()) {
				return true;
			}

			index++;
		}

		return false;
	}

	public String getMonthName() {
		return new Month(_monthOfYear).getShortName();
	}

	public String getWeekName() {
		return "Week " + _weekOfMonth;
	}

	public Day(int dayOfWeek) {
		_dayOfWeek = dayOfWeek;
	}

	public int getDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDate());
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	public void setDayOfWeek(int _dayOfWeek) {
		this._dayOfWeek = _dayOfWeek;
	}

	public int getWeekOfMonth() {
		return _weekOfMonth;
	}

	public void setWeekOfMonth(int _weekOfMonth) {
		this._weekOfMonth = _weekOfMonth;
	}

	public int getMonthOfYear() {
		return _monthOfYear;
	}

	public void setMonthOfYear(int _monthOfYear) {
		this._monthOfYear = _monthOfYear;
	}

	@Override
	public void setName(String name) {
	}

	@Override
	public String getName() {
		return DateUtils.getDayOfWeekString(getDayOfWeek(),
				DateUtils.LENGTH_LONG);
	}

	@Override
	public void setId(long _id) {
	}

	@Override
	public long getId() {
		return getDayOfWeek();
	}

	@Override
	public String toString() {
		return getName() + "," + getDayOfMonth() + " " + getMonthName() + " " + getYear();
	}

	public String getShortestName() {
		return DateUtils.getDayOfWeekString(getDayOfWeek(),
				DateUtils.LENGTH_SHORTEST);
	}

	public String getShortName() {
		return DateUtils.getDayOfWeekString(getDayOfWeek(),
				DateUtils.LENGTH_SHORT);
	}

	public static int year() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	
	public static int month() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.MONTH);
	}	
	
	public static int dayOfMonth() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_MONTH);
	}	
	
	public static Date now() {
		return new Date(year(),month(),dayOfMonth());
	}

	public void setDayOfMonth(int _dayOfMonth) {
		this._dayOfMonth = _dayOfMonth;
	}

	public int getDayOfMonth() {
		return _dayOfMonth;
	}

	public void setYear(int _year) {
		this._year = _year;
	}

	public int getYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	
	public Date getDate() {
		//Log.i("Day", "getting date for day....." + getName());
		//Log.i("Day", "getYear.................." + getYear());
		return new Date(getYear(),getMonthOfYear(),getDayOfMonth());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + _dayOfMonth;
		result = prime * result + _dayOfWeek;
		result = prime * result + _monthOfYear;
		result = prime * result + _weekOfMonth;
		result = prime * result + _year;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Day other = (Day) obj;
		if (_dayOfMonth != other._dayOfMonth)
			return false;
		if (_dayOfWeek != other._dayOfWeek)
			return false;
		if (_monthOfYear != other._monthOfYear)
			return false;
		if (_weekOfMonth != other._weekOfMonth)
			return false;
		if (_year != other._year)
			return false;
		return true;
	}
}